using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using _3Dto2D.Configuration;
using _3Dto2D.Interfaces;
using _3Dto2D.Services;
using _3Dto2D.ViewModels;

namespace _3Dto2D;

public static class ServiceConfiguration
{
    public static IServiceCollection ConfigureServices(this IServiceCollection services)
    {
        // Configuration
        services.AddSingleton<ConfigurationManager>();
        services.AddSingleton(provider =>
        {
            var configManager = provider.GetRequiredService<ConfigurationManager>();
            return configManager.Settings;
        });

        // Logging
        services.AddLogging(builder =>
        {
            builder.ClearProviders();
            builder.AddFile("3Dto2D_log.txt");
            builder.SetMinimumLevel(LogLevel.Information);
        });

        services.AddSingleton<App>();



        // Core services
        services.AddSingleton<IRenderEngine, ModernRenderEngine>();
        services.AddSingleton<ILibraryManager, ModernLibraryManager>();
        services.AddSingleton<ExportService>();

        // Object pools
        services.AddSingleton<BitmapPool>();
        services.AddSingleton<MemoryStreamPool>();

        // ViewModels
        services.AddSingleton<MainViewModel>();

        // Progress reporting
        services.AddTransient(typeof(ProgressReporter<>));
        services.AddTransient(typeof(BatchProcessor<,>));

        return services;
    }

    public static IServiceProvider BuildServiceProvider()
    {
        var services = new ServiceCollection();
        services.ConfigureServices();

        var serviceProvider = services.BuildServiceProvider();

        // Async initialization can be done in the form's Load event
        return serviceProvider;
    }
}
